package com.Lixiaoqian.Gulangyu.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.Lixiaoqian.Gulangyu.R
import lixiaoqian.com.skinproject.utils.DensityUtil

/**
 * 指示器
 * Created by huangzhibo on 2018-02-28.
 * mail:1043202454@qq.com
 */
class IndictorLayout(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
    var num: Int = 0
    var select: Int = 0
    var indicSizes = 0.0f
    var ofest=0.0f
    var backColor:Int = 0
    var selectColor:Int = 0
    lateinit var paint: Paint

    init {
        paint = Paint()
        paint.isAntiAlias = true
        paint.style = Paint.Style.FILL
        ofest=DensityUtil.dip2px(context!!,2f).toFloat()

        val attributes = context?.obtainStyledAttributes(attrs, R.styleable.IndictorLayout)
        backColor= attributes.getColor(R.styleable.IndictorLayout_indicBackColor,Color.WHITE)
        selectColor= attributes.getColor(R.styleable.IndictorLayout_indicSelectColor,Color.GRAY)
        indicSizes= attributes.getDimension(R.styleable.IndictorLayout_indicSize,10f)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        if (num != 0) {
            val size = indicSizes * num
            setMeasuredDimension(size.toInt(),MeasureSpec.getSize(heightMeasureSpec))
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        if (num != 0) {
            for (i in 0..num-1) {
                if (i == select) {
                    paint.color = selectColor
                } else {
                    paint.color = backColor
                }
                if (i==0){
                    canvas?.drawCircle(indicSizes/2,(this.height/2).toFloat(),indicSizes/2-ofest,paint)
                }else{
                    canvas?.drawCircle(indicSizes/2+i*indicSizes,(this.height/2).toFloat(),indicSizes/2-ofest,paint)
                }

            }
        }
    }

    fun setSelectNum(select: Int) {
        this.select = select%num
        invalidate()
    }

    fun setIndicSize(size: Float) {
        indicSizes = DensityUtil.dip2px(context, size).toFloat()
    }

    fun <T> setDataList(data: List<T>) {
        this.num = data.size
        invalidate()
    }
}